package com.hzdesk.media.koreabatchmatvector.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.config.AppConfig;

import java.io.File;

/**
 * Created by Administrator on 2018/6/20.
 * 杭州席媒科技有限公司  Developers  刘传华  16
 * 佛祖镇楼 bug辟易
 */

public class SharedPreferencesUtils {

    private static volatile SharedPreferencesUtils mSharedPreferencesUtils = null;
    private static SharedPreferences mSharedPreferences;
    private static SharedPreferences.Editor editor;

    public SharedPreferencesUtils(Context context) {
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        editor = mSharedPreferences.edit();
    }

    public static SharedPreferencesUtils getInstance(Context context) {
        if (mSharedPreferencesUtils == null) {
            synchronized (SharedPreferencesUtils.class) {
                if (mSharedPreferencesUtils == null) {
                    mSharedPreferencesUtils = new SharedPreferencesUtils(context);
                }
            }
        }
        return mSharedPreferencesUtils;
    }

    /**
     * @desc 向sp中添加数据
     */
    public void putDate(String key, Object obj) {
        // key 不为null时再存入，否则不存储
        if (key != null) {
            if (obj instanceof Integer) {
                editor.putInt(key, (Integer) obj);
            } else if (obj instanceof Long) {
                editor.putLong(key, (Long) obj);
            } else if (obj instanceof Boolean) {
                editor.putBoolean(key, (Boolean) obj);
            } else if (obj instanceof Float) {
                editor.putFloat(key, (Float) obj);
            } else if (obj instanceof String) {
                editor.putString(key, (String) obj);
            }
            editor.apply();
        }
    }

    /**
     * @desc 根据key 获取对应sp的值
     */
    public Object getDate(String key, String type) {
        switch (type) {
            case "String":
                return mSharedPreferences.getString(key, "");
            case "Integer":
                return mSharedPreferences.getInt(key, 0);
            case "Boolean":
                return mSharedPreferences.getBoolean(key, false);
            case "Float":
                return mSharedPreferences.getFloat(key, 0);
            case "Long":
                return mSharedPreferences.getLong(key, 0);
        }
        return null;
    }

    /**
     * @desc 清空sp中的数据
     */
    public void clear() {
        editor.clear().commit();
    }

    public void clearKey(String key) {
        editor.remove(key);
        editor.commit();
    }

    /**
     * 获取视频保存路径
     *
     * @param context
     * @return
     */
    public static String getVideoSavePath(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        String videoSavePath = sp.getString(AppConfig.Video_SAVE_PATH, AppConfig.DESKMEDIAFILE);
        return videoSavePath;
    }

    /**
     * 获取截屏保存路径
     *
     * @param context
     * @return
     */
    public static String getScreenSavePath(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        String screenSavePath = sp.getString(AppConfig.SCREEN_SAVE_PATH, AppConfig.DESKMEDIAFILE);
        return screenSavePath;
    }

    /**
     * 获取会议名称
     *
     * @param context
     * @return
     */
    public static String getMeetingName(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        String meetingName = sp.getString(AppConfig.MEETING_NAME, AppConfig.MEETING_NAME_DEFAULT);
        return meetingName;
    }

    /**
     * 是否显示批注
     *
     * @param context
     * @return
     */
    public static boolean isShowCanvas(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        boolean b = sp.getBoolean(AppConfig.SHOW_CANVAS, true);
        return b;
    }

    /**
     * 获取录制分段时长
     *
     * @param context
     * @return
     */
    public static long getSubSectionTime(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        long time = sp.getLong(AppConfig.OPTION_SUBSETION_TIME, AppConfig.DefaultSubSectionTime);
        return time;
    }

    //获取录制模式(1:video+audio  0:video)
    public static int getRecordMode(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        int mode = sp.getInt(AppConfig.OPTION_RECORD_MODE, AppConfig.DefaultRecordMode);
        return mode;
    }

    //获取使用模式
    public static int getUserMode(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        int mode = sp.getInt(AppConfig.OPTION_USER_MODE, AppConfig.DefaultUserMode);
        return mode;
    }

    //获取协议 (1:HDMI IN 0:Miracast)
    public static int getProtocal(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        int mode = sp.getInt(AppConfig.OPTION_PROTOCOL, AppConfig.DefaultProtocal);
        return mode;
    }

    //获取光标颜色
    public static int getCursorColor(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        int mode = sp.getInt(AppConfig.OPTION_CURSOR_COLOR, AppConfig.DefaultCursorColor);
        return mode;
    }

    //获取笔触大小
    public static int getPathSize(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        int mode = sp.getInt(AppConfig.OPTION_path_size, AppConfig.DefaultPathSize);
        return mode;
    }

    //判断首运行
    public static boolean isFirstRun(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        boolean isFirstRun = sp.getBoolean(AppConfig.IS_FIRST_RUN, true);
        return isFirstRun;
    }

    // 获取水印路径
    public static String getWaterMarkPath(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        String Path = sp.getString(AppConfig.WATER_MARK_PATH, AppConfig.DEFAULT_WATER_MARK_PATH);
        return Path;
    }


}
